<?php
	require_once("konf.php");
	
	if(isSet($_REQUEST["talli_nimi"])){
		$kask=$yhendus->prepare("SELECT talli_nimi, tall_ID FROM tallid WHERE talli_nimi=?");
		$kask->bind_param("s", $_REQUEST["talli_nimi"]);
		$kask->bind_result($talli_nimi, $tall_ID);
		$kask->execute();
	}
	
	if(isSet($_REQUEST["sisend_latter"])){
		$kask=$yhendus->prepare(
        "INSERT INTO latrid(latri_nr, tall_ID) VALUES (?, ?)");
		$kask->bind_param("ii", $_REQUEST["latri_nr"], $_REQUEST["tall_ID"]);
		$kask->execute();
		$yhendus->close();
		header("Location: $_SERVER[PHP_SELF]?lisatudlatter=$_REQUEST[latri_nr]");
		exit();
	}
	
?>
<html>
	<head>
		<title>Talli andmed</title>
		<link href="kujundus.css" rel="stylesheet" type="text/css" />
		<meta charset="UTF-8">
	</head>
	<body>
	<div id="pealkiri">
	<a href="haldusleht.php">Halduslehele</a>
	<?php
		while(@$kask->fetch()){
		    $talli_nimi=htmlspecialchars($talli_nimi);
		    echo "
			  <h1>$talli_nimi</h1>
			";
		 }
		 $kask->close();
	?> 
	</div>
	<div id="main_container">
	<h2>Latrid:</h2>
		<table>
			<tr>
				<th>ID</th>
				<th>Number</th>
			</tr>
	<?php
		if(isSet($_REQUEST["talli_nimi"])){
			$kask=$yhendus->prepare("SELECT talli_nimi, latri_nr, latter_ID FROM tallid, latrid WHERE latrid.tall_ID=tallid.tall_ID AND talli_nimi=?
									ORDER BY latri_nr
									" );
				$kask->bind_param("s", $_REQUEST["talli_nimi"]);
				$kask->bind_result($talli_nimi, $latri_nr, $latter_ID);
				$kask->execute();	
				while($kask->fetch()){
				$tall_ID=htmlspecialchars($latri_nr);
				echo "
					<tr>
						<td>$latter_ID</td>
						<td>$latri_nr</td>
					</tr>  
					";
				}
		}
	?>
		</table>
	<?php
			if(isSet($_REQUEST["lisatudlatter"])){
			echo "Aitäh! $_REQUEST[lisatudlatter] lisatud!";
			}
	?>
	<form action="?">
		<input type="hidden" name="sisend_latter" value="jah" />
		<input type="hidden" name="tall_ID" value="<?php echo htmlspecialchars($tall_ID); ?>" />
		<dl>
			<dt>Number: </dt>
			<tr><input type="text" name="latri_nr" /></tr>
			<dt><input type="submit" name="sisestusnupp" value="Lisa latter" /></dt>
		</dl>
	</form>
	</div>
	</body>
</html>